﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GetWebInfo
{
    /// <summary>
    /// 市值
    /// </summary>
    class MarketCapital
    {
        public MarketCapital(SharePrice sp, Split split)
        {
            this._sp = sp;
            this._split = split;


            /*
             * 由于两个循环嵌套情况比较复杂，因此规定SharePrice与Split文件只包含下面两种合理的情况
             * 
             * SPL的末尾超过SL，SL的头超过SPL
             * SharePriceList：             SplitList:
             * ---------------              
             * ---------------              
             * ---------------              ---------------     //次日期前的股数都为SL
             * ---------------      
             * ---------------  
             * ---------------              ---------------     //次日期前的股数都为SL
             * ---------------
             * ---------------
             * ---------------
             *                              ---------------     //次日期前的股数都为SL
             * 
             * SL可以通过获取新浪的价格，在参考新浪的股本结构编写
             */

            ulong _tsplit = 0;
            bool _find = false;
            foreach (KeyValuePair<DateTime, decimal> kvpSP in sp.Prices)
            {
                foreach (KeyValuePair<DateTime, ulong> kvpSplit in split.Splits)
                {
                    if (kvpSP.Key <= kvpSplit.Key)
                    {
                        this.Capitals.Add(kvpSP.Key, kvpSP.Value * decimal.Parse(kvpSplit.Value.ToString()));
                        this.Shares.Add(kvpSP.Key, kvpSplit.Value);
                        _find = true;
                        break;
                    }
                    else if (kvpSP.Key > kvpSplit.Key)
                    {
                        _tsplit = kvpSplit.Value;
                        continue;
                    }
                }

                if (!_find)
                {
                    this.Capitals.Add(kvpSP.Key, kvpSP.Value * decimal.Parse(_tsplit.ToString()));
                    this.Shares.Add(kvpSP.Key, _tsplit);
                }
                _find = false;
            }
        }

        private SharePrice _sp;
        private Split _split;

        /// <summary>
        /// SharePrice下所有的市值
        /// </summary>
        public SortedList<DateTime, decimal> Capitals = new SortedList<DateTime, decimal>();

        /// <summary>
        /// SharePrice下所有的股份数
        /// </summary>
        public SortedList<DateTime, ulong> Shares = new SortedList<DateTime, ulong>();


        /// <summary>
        /// 获取每年的平均市值
        /// </summary>
        /// <returns></returns>
        public SortedList<int, decimal> GetAvgValues
        {
            get
            {
                SortedList<int, decimal> _sdv = new SortedList<int, decimal>();

                int _year = 0;
                int _count = 0;
                decimal _sum = 0;

                foreach (KeyValuePair<DateTime, decimal> kvp in this.Capitals)
                {
                    if (_year == 0 ||
                        _year != kvp.Key.Year)
                    {
                        if (_count != 0)
                        {
                            _sdv.Add(_year, _sum / _count);
                        }
                        _year = kvp.Key.Year;
                        _count = 1;
                        _sum = kvp.Value;
                    }
                    else
                    {
                        _count++;
                        _sum += kvp.Value;
                    }
                }

                _sdv.Add(_year, _sum / _count);

                return _sdv;
            }
        }

        /// <summary>
        /// 获取每年的最大市值
        /// </summary>
        /// <returns></returns>
        public SortedList<int, decimal> GetMaxValues
        {
            get
            {
                SortedList<int, decimal> _sdv = new SortedList<int, decimal>();

                int _year = 0;
                decimal _max = 0;
                int i = 0;

                foreach (KeyValuePair<DateTime, decimal> kvp in this.Capitals)
                {
                    if (i == 0)
                    {
                        i++;
                        _year = kvp.Key.Year;                        
                        _max = kvp.Value;
                        continue;
                    }
                    else if (_year == kvp.Key.Year)
                    {
                        if (_max < kvp.Value)
                        {
                            _max = kvp.Value;
                        }
                    }
                    else if (_year != kvp.Key.Year)
                    {
                        _sdv.Add(_year, _max);
                        _year = kvp.Key.Year;
                        _max = kvp.Value;
                    }
                }

                _sdv.Add(_year, _max);
                return _sdv;
            }
        }

        /// <summary>
        /// 获取每年的最小市值
        /// </summary>
        /// <returns></returns>
        public SortedList<int, decimal> GetMinValues
        {
            get
            {
                SortedList<int, decimal> _sdv = new SortedList<int, decimal>();

                int _year = 0;
                decimal _max = 0;
                int i = 0;

                foreach (KeyValuePair<DateTime, decimal> kvp in this.Capitals)
                {
                    if (i == 0)
                    {
                        i++;
                        _year = kvp.Key.Year;
                        _max = kvp.Value;
                        continue;
                    }
                    else if (_year == kvp.Key.Year)
                    {
                        if (_max > kvp.Value)
                        {
                            _max = kvp.Value;
                        }
                    }
                    else if (_year != kvp.Key.Year)
                    {
                        _sdv.Add(_year, _max);
                        _year = kvp.Key.Year;
                        _max = kvp.Value;
                    }
                }

                _sdv.Add(_year, _max);
                return _sdv;
            }
        }
    }
}
